package dao;

import bean.Student;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;
import java.util.Scanner;

public class hr {
    public static void insert() {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        SqlSession sqlSession =(SqlSession) context.getBean("sqlSession");
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入name");
        String a=sc.next();
        Student student=new Student();
        student.setName(a);
        student.setIscall(0);
        int insert = studentMapper.insert(student);
        System.out.println("Affected rows: " + insert);
    }
    public static void selectAll(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        SqlSession sqlSession =(SqlSession) context.getBean("sqlSession");
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        List<Student> studentList=studentMapper.selectList(null);
        for(int i=0;i<studentList.size();i++){
            System.out.println("id: "+studentList.get(i).getId()+"    姓名： "+studentList.get(i).getName());
        }
    }
    public static void delete(int id){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        SqlSession sqlSession =(SqlSession) context.getBean("sqlSession");
        StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
        int d=studentMapper.deleteById(id);
        System.out.println("Affected rows: " + d);
    }
}
